package com.hzsk.lisence.controller;

import com.hzsk.lisence.config.myexception.BaseException;
import com.hzsk.lisence.config.myexception.ParameterException;
import com.hzsk.lisence.entity.LisenceUserEntity;
import com.hzsk.lisence.entity.LisenceUserGrouping;
import com.hzsk.lisence.entity.UserRole;
import com.hzsk.lisence.entity.query.LisenceServiceQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.LisenceUserMapper;
import com.hzsk.lisence.service.UserService;
import com.hzsk.lisence.util.EncipherUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/lisence/userinfo/")
public class LisenceUserController {

    @Resource
    public UserService userService;

    @Resource
    private LisenceUserMapper lisenceUserMapper;


    @PostMapping("userlogin")
    @ApiOperation(value = "用户登录")
    public IncResponce userLogin(@RequestBody LisenceUserEntity lisenceUserEntity){
        IncResponce incResponce = new IncResponce();
        try {
            if (StringUtils.isEmpty(lisenceUserEntity.getUserAccount()) || StringUtils.isEmpty(lisenceUserEntity.getPassword())) {
                throw new ParameterException("请求参数异常");
            }else {
                LisenceUserEntity entity = lisenceUserMapper.selectEntityByAccountAndPassword(lisenceUserEntity);
                if (entity != null) {
                    String decrypt = EncipherUtils.decrypt(entity.getPassword());
                    if (decrypt.equals(lisenceUserEntity.getPassword())){
                        incResponce.setData(userService.userLogin(entity,entity.getUserId()));
                        incResponce.setCode(0);
                        return incResponce;
                    }
                }
            }
            incResponce.setErrorMessage("账号或密码错误");
            incResponce.setCode(1);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("getuserlist")
    @ApiOperation(value = "查看用户列表")
    public IncResponce getUserList(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            userService.getUserList(lisenceServiceQuery,incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("createuserbyadmin")
    @ApiOperation(value = "创建用户")
    public IncResponce createUserByAdmin(@RequestBody LisenceUserEntity lisenceUserEntity){
        IncResponce incResponce = new IncResponce();
        try {
           synchronized (this){
               String userRole = lisenceUserEntity.getUserRole();
               String password = lisenceUserEntity.getPassword();
               String userAccount = lisenceUserEntity.getUserAccount();
               String userName = lisenceUserEntity.getUserName();
               String userNum = lisenceUserEntity.getUserNum();
               if(StringUtils.isEmpty(userNum)){
                   throw new BaseException("用户工号不能为空");
               }
               LisenceUserEntity user = lisenceUserMapper.selectEntityByUserNum(lisenceUserEntity);
               if (!ObjectUtils.isEmpty(user)){
                   throw new BaseException("用户工号已存在");
               }
               if (!userRole.equals(UserRole.ORDINARY.toString())){
                   if ((StringUtils.isBlank(userAccount) || StringUtils.isBlank(password) || StringUtils.isBlank(userName))){
                       incResponce.setCode(1);
                       incResponce.setErrorMessage("请求参数异常");
                       return incResponce;
                   }else {
                       Integer account = lisenceUserMapper.isAccount(userAccount);
                       if (account > 0){
                           incResponce.setCode(1);
                           incResponce.setErrorMessage("用户已存在");
                           return incResponce;
                       }
                       else if(userName.equals("超级管理员"))
                       {
                           incResponce.setCode(1);
                           incResponce.setErrorMessage("不能创建超级管理员");
                           return incResponce;
                       }
                   }
               }else {
                   if (StringUtils.isBlank(userName)){
                       incResponce.setCode(1);
                       incResponce.setErrorMessage("请求参数异常");
                       return incResponce;
                   }
                   List<LisenceUserEntity> lisenceUserEntities = lisenceUserMapper.selectEntityByName(userName);
                   if (lisenceUserEntities.size() > 0){
                       incResponce.setCode(1);
                       incResponce.setErrorMessage("客户名称不能重复");
                       return incResponce;
                   }
               }
               incResponce.setCode(0);
               incResponce.setData(userService.saveLisenceUser(lisenceUserEntity,lisenceUserEntity.getOperatorUserId()));
           }
        } catch (Exception e) {
            incResponce.setCode(1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PutMapping("updateuserinfo")
    @ApiOperation(value = "修改用户信息")
    public IncResponce updateUserInfo(@RequestBody LisenceUserEntity lisenceUserEntity){
        IncResponce incResponce = new IncResponce();
        try {
            userService.updateUserInfo(lisenceUserEntity,lisenceUserEntity.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @DeleteMapping("deluserinfo")
    @ApiOperation(value = "删除用户信息")
    public IncResponce delUserinfo(@RequestParam("id")Integer id,@RequestParam("operatorUserId")Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(userService.delUserinfo(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @GetMapping("setpasswordreset")
    @ApiOperation(value = "用户密码重置")
    public IncResponce setPasswordReset(@RequestParam("id")Integer id,@RequestParam("operatorUserId")Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(userService.setPasswordReset(id,operatorUserId));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PostMapping("creategroupingbyadmin")
    @ApiOperation(value = "创建组织信息")
    public IncResponce createGroupingByAdmin(@RequestBody LisenceUserGrouping lisenceUserGrouping){
        IncResponce incResponce = new IncResponce();
        try {
            userService.createGroupingByAdmin(lisenceUserGrouping,lisenceUserGrouping.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }

    @PostMapping("getusergroupinglist")
    @ApiOperation(value = "查看组织列表")
    public IncResponce getUserGroupingList(@RequestBody LisenceServiceQuery lisenceServiceQuery){
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setCode(0);
            incResponce.setData(userService.getUserGroupingList(lisenceServiceQuery));
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @PutMapping("updateusergroupinginfo")
    @ApiOperation(value = "修改组织信息")
    public IncResponce updateUserGroupingInfo(@RequestBody LisenceUserGrouping lisenceUserGrouping){
        IncResponce incResponce = new IncResponce();
        try {
            userService.updateUserGroupingInfo(lisenceUserGrouping,lisenceUserGrouping.getOperatorUserId(),incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }


    @DeleteMapping("delusergroupinginfo")
    @ApiOperation(value = "删除组织")
    public IncResponce delUserGroupingInfo(@RequestParam("id")Integer id,@RequestParam("operatorUserId")Integer operatorUserId){
        IncResponce incResponce = new IncResponce();
        try {
            userService.delUserGroupingInfo(id,operatorUserId,incResponce);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.toString());
            e.printStackTrace();
        }
        return incResponce;
    }










}
